<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .el-table .warning-row {
            background: oldlace;
        }

        .el-table .success-row {
            background: #f0f9eb;
        }
    </style>

</head>
<body>
<div id="app">

    <!--按钮-->

    <el-row>

        <el-button type="primary" plain @click="dialogVisible = true">新增</el-button>

    </el-row>
    <!--添加数据对话框表单-->
    <el-dialog
            title="新增留言"
            :visible.sync="dialogVisible"
            width="30%"
            >

        <el-form ref="form" :model="brand" label-width="80px">
            <el-form-item label="留言人">
                <el-input v-model="brand.brandName"></el-input>
            </el-form-item>

            <el-form-item label="留言内容">
                <el-input v-model="brand.companyName"></el-input>
            </el-form-item>



            <el-form-item>
                <el-button type="primary" @click="addBrand">提交</el-button>
                <el-button @click="dialogVisible = false">取消</el-button>
            </el-form-item>
        </el-form>

    </el-dialog>

    <!--表格-->
    <template>
        <el-table
                :data="tableData"
                style="width: 100%"
                :row-class-name="tableRowClassName"
                @selection-change="handleSelectionChange"
        >
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column
                    type="index"
                    width="50">
            </el-table-column>

            <el-table-column
                    prop="brandName"
                    label="留言人"
                    align="center">
            </el-table-column>
            <el-table-column
                    prop="companyName"
                    label="留言内容"
                    align="center">

            </el-table-column>
            <el-table-column
                    prop="status"
                    label="留言时间"
                    align="center">
            </el-table-column>

        </el-table>
    </template>

    <!--分页工具条-->
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="5"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalCount">
    </el-pagination>

</div>


<script src="js/vue.js"></script>
<script src="element-ui/lib/index.js"></script>
<link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">

<script src="js/axios-0.18.0.js"></script>

<script>
    new Vue({
        el: "#app",

        mounted(){
            //页面加载完之后发送异步请求获取数据
            this.selectAll();
            // var _this = this;
            // axios({
            //     method:"get",
            //     url:"http://localhost:8080/javaweb/selectAllServlet"
            // }).then(function (resp){
            //     _this.tableData = resp.data;
            // })
        },
        methods: {

            //查询分页数据
            selectAll(){
                var _this = this;
                axios({
                    method:"get",
                    url:"http://localhost:8080/javaweb/selectByPageServlet?currentPage="+_this.currentPage+"&pageSize="+_this.pageSize
                }).then(function (resp){
                    _this.tableData = resp.data.rows;
                    _this.totalCount = resp.data.totalCount;
                })
            },
            tableRowClassName({row, rowIndex}) {
                if (rowIndex === 1) {
                    return 'warning-row';
                } else if (rowIndex === 3) {
                    return 'success-row';
                }
                return '';
            },
            // 复选框选中后执行的方法
            handleSelectionChange(val) {
                this.multipleSelection = val;

                console.log(this.multipleSelection)
            },
            // 查询方法
            onSubmit() {
                console.log(this.brand);
            },
            // 添加数据
            addBrand(){
                // console.log(this.brand);
                var _this = this;
                axios({
                    method: "post",
                    url:"http://localhost:8080/javaweb/addServlet",
                    data:_this.brand
                }).then(function (resp){
                    if(resp.data == 'success'){//就是success那个字符串,data意思就是resp里面的内容
                        //添加成功
                        //关闭窗口
                        _this.dialogVisible = false;
                    }else{
                        alert("xss攻击")
                    }
                })
            },

            //分页
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
            },
            handleCurrentChange(val) {
                //console.log(`当前页: ${val}`);
                this.currentPage = val;
                this.selectAll();
            }

        },
        data() {
            return {
                pageSize:5,
                //总记录数
                totalCount:100,
                // 当前页码
                currentPage: 1,
                // 添加数据对话框是否展示的标记
                dialogVisible: false,

                // 品牌模型数据
                brand: {
                    status: '',
                    brandName: '',
                    companyName: '',
                    id:"",
                    ordered:"",
                    description:""
                },
                // 复选框选中数据集合
                multipleSelection: [],
                // 表格数据
                tableData: [{
                    brandName: '小明',
                    companyName: '你好',
                    ordered: '100',
                    status: "1"
                }, {
                    brandName: '小华',
                    companyName: '你好吗',
                    ordered: '100',
                    status: "1"
                }, {
                    brandName: '小丽',
                    companyName: '我叫小丽',
                    ordered: '100',
                    status: "1"
                }, {
                    brandName: '小莉',
                    companyName: '找小莉帮忙',
                    ordered: '100',
                    status: "1"
                }]
            }
        }
    })

</script>

</body>
</html>